export function passwordValidator(userName, pwdConfig, rule, value, callback) {
  if (pwdConfig == null) {
    callback(new Error('密码规则获取失败'))
  }
  const validateMessage = {
    contain: [],
    length: [],
    notContain: []
  }
  // 启用验证
  if (pwdConfig.strengthLimit) {
    if (pwdConfig.needLength && pwdConfig.minLength > 0) {
      validateMinLength(value, pwdConfig.minLength, callback, validateMessage.length)
    }
    if (pwdConfig.needDigit) {
      validateDigit(value, callback, validateMessage.contain)
    }
    if (pwdConfig.needLowerCase) {
      validateLowerCase(value, callback, validateMessage.contain)
    }
    if (pwdConfig.needUpperCase) {
      validateUpperCase(value, callback, validateMessage.contain)
    }
    if (pwdConfig.needSpecialCharacter) {
      validateSpecialCharacter(value, callback, validateMessage.contain)
    }
    if (pwdConfig.notUserName) {
      validateNotUserName(userName, value, callback, validateMessage.notContain)
    }
  }
  if (validateMessage.contain.length < 1 && validateMessage.length.length < 1 && validateMessage.notContain.length < 1) {
    callback()
  } else {
    const lengthErrorMsg = validateMessage.length.length > 0 ? validateMessage.length.join('') : ''
    const containErrorMsg = validateMessage.contain.length > 0 ? ('需要包含' + validateMessage.contain.join('、')) : ''
    const notContainErrorMsg = validateMessage.notContain.length > 0 ? ('不能包含' + validateMessage.notContain.join('')) : ''

    let errMsg = '密码'
    if (lengthErrorMsg) {
      errMsg = errMsg + lengthErrorMsg + '，'
    }
    if (containErrorMsg) {
      errMsg = errMsg + containErrorMsg + '，'
    }
    if (notContainErrorMsg) {
      errMsg = errMsg + notContainErrorMsg + '，'
    }
    errMsg = errMsg.slice(0, -1)
    callback(new Error(errMsg))
  }
}

function validateMinLength(newPassword, minLength, callback, message) {
  if (newPassword == null || newPassword.length < minLength) {
    message.push(`长度不小于${minLength}`)
  }
}
function validateDigit(newPassword, callback, message) {
  if (newPassword == null || newPassword.match(/.*\d.*/) == null) {
    message.push('数字')
  }
}
function validateLowerCase(newPassword, callback, message) {
  if (newPassword == null || newPassword.match(/.*[a-z].*/) == null) {
    message.push('小写字母')
  }
}
function validateUpperCase(newPassword, callback, message) {
  if (newPassword == null || newPassword.match(/.*[A-Z].*/) == null) {
    message.push('大写字母')
  }
}
function validateSpecialCharacter(newPassword, callback, message) {
  if (newPassword == null || newPassword.match(/.*[^A-Za-z0-9].*/) == null) {
    message.push('特殊字符')
  }
}
function validateNotUserName(userName, newPassword, callback, message) {
  if (!userName) {
    return
  }
  const regExp = new RegExp(`.*${userName.toLowerCase()}.*`)
  if (newPassword == null || newPassword.toLowerCase().match(regExp) != null) {
    message.push('用户名')
  }
}
